/* Author: Mark Mckillion
 * Student ID: B00540772
 * Date: 07/03/2012
 * Version 1.0.*/
package assignment3;

public class RedrawThread implements Runnable
{   /*Create a class that will use Runnable to override run. This will be used 
     *to update the GUI at a defined interval.*/
    
    //Create the variables and objects that will be used within the Thread.
    private DrawingPanel panel;
    private long time;
    private int num;
    private BankAccount acc;
    
    /*Create a constructor that will accept the parameters needed to update and
     *output information.*/
    public RedrawThread(DrawingPanel pane, long num1, int num2, BankAccount account)
    {   //Make the class variables equal the parameter variables.
        panel = pane;
        time = num1;
        num  = num2;
        acc = account;
    }
    
    @Override
    public void run()
    {   /*Use a try-catch block which will throw an Exception if the Thread
         *cannot be executed.*/
        try 
        {   while(true)
            {   /*while the Thread is valid set the balance and duration within
                 *the panel to the same as in the account. Once integers are
                 *the same revalidate and repaint the GUI, pause until 'time'
                 *has expired*/
                num ++;
                panel.setBalance(acc.getBalance());
                //times the number by 9 as each second is 9 pixels on the graph
                panel.setDuration(num);
                panel.revalidate();
                panel.repaint();
                Thread.sleep(time); 
            }
        }   catch (InterruptedException ex)
            {   //Stop the thread if not valid, no output required.
            }
     }
} // End of class RedrawThread